package erfgame.core.graphics.renderer;

import java.util.HashMap;


import erfgame.core.CompassDirection;
import erfgame.core.Rotation;
import erfgame.core.entity.Entity;
import erfgame.core.entity.State;
import erfgame.core.entity.StatefulEntity;
import erfgame.core.world.World;


public class DynamicStatefulEntityRenderer extends StatefulEntityRenderer {
	
	// default compass direction since our entity is non-orientable (presumably)
	private CompassDirection orientation;
	private RenderEntityRendererFactoryAdapter rendererFactory;
	private Rotation viewDirection;
	
	public DynamicStatefulEntityRenderer( 
			World world, 
			StatefulEntity entity, 
			RenderEntityRendererFactoryAdapter rendererFactory, 
			EntityRenderer defaultRenderer, 
			CompassDirection orientation, 
			Rotation viewDirection ) {
		super( world, entity, new HashMap<State, EntityRenderer>(), defaultRenderer );
		this.rendererFactory = rendererFactory;
		this.orientation = orientation;
		this.viewDirection = viewDirection;
	}

	@Override
	public EntityRenderer getRenderer(State state) {
		EntityRenderer renderer = this.renderers.get(state);
		if( renderer == null ) {
			// TODO this is a high-latency operation, is going to need to be threaded/handled some way
			renderer = this.rendererFactory.getRenderer(world, getEntity(), state, orientation, 0, viewDirection);
			this.renderers.put(state, renderer);
		}
		return renderer;
	}

	@Override
	public EntityRenderer spawn(World world, Entity entity) {
		return new DynamicStatefulEntityRenderer( 
				world, 
				(StatefulEntity)entity, 
				this.rendererFactory,
				((PrototypeEntityRenderer)this.defaultRenderer).
				spawn(world, entity), 
				this.orientation, 
				this.viewDirection
		);
	}
}
